<script src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<?php
$appid='wxf59ec98db11346b5';
$appsecret='de1fd9024a6af208fd69cc72351d62fb';
$timestamp = time();
$nonce = rand(1000,999999);
$title='不知中山路，别谈锡城繁华';
$desc='一条中山路，点亮一城芳华';
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$link = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$image='http://lighter.longyue8.com/styles/img/share.jpg';

$ticket='';
if(file_exists('./jsapi_ticket.json')){
    $data = json_decode(file_get_contents("./jsapi_ticket.json"));
    if($data->expire_time>time()){
        $ticket = $data->jsapi_ticket;
    }
}
if(!$ticket){
    $accesstoken='';
    if(file_exists('./accesstoken.json')){
        $data=json_decode(file_get_contents('./accesstoken.json'));
        if($data && $data->expire_time>time()){
            $accesstoken=$data->access_token;
        }
    }
    if(!$accesstoken){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
        $res = file_get_contents($url);
        if($res){
            $res = json_decode($res);
            if(isset($res->access_token)){
                $accesstoken = $res->access_token;
                if($accesstoken){
                    $data->expire_time = time()+7000;
                    $data->access_token = $accesstoken;
                    $fp = fopen('./accesstoken.json','w');
                    fwrite($fp,json_encode($data));
                    fclose($fp);
                }
            }
        }
    }
    if($accesstoken){
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accesstoken";
        $res = json_decode(file_get_contents($url));
        $ticket = $res->ticket;
        if ($ticket) {
            $data->expire_time = time() + 7000;
            $data->jsapi_ticket = $ticket;
            $fp = fopen('./jsapi_ticket.json', "w");
            fwrite($fp, json_encode($data));
            fclose($fp);
        }
    }
}

if($ticket){
    $string = "jsapi_ticket=$ticket&noncestr=$nonce&timestamp=$timestamp&url=$link";
    $sign = sha1($string);
}



?>
<script>
    <?php if($sign): ?>
    wx.config({
        debug: false,
        appId: '<?= $appid ?>', // 必填，公众号的唯一标识
        timestamp: <?= $timestamp ?>, // 必填，生成签名的时间戳
        nonceStr: '<?= $nonce ?>', // 必填，生成签名的随机串
        signature: '<?= $sign ?>',// 必填，签名
        jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填，需要使用的JS接口列表
    });
    wx.ready(function () {
        wx.onMenuShareTimeline({
            title: '<?= $title ?>', // 分享标题
            link: '<?= $link ?>', // 分享链接
            imgUrl: '<?= $image ?>', // 分享图标
            success: function () {
                //alert('sss');
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
        wx.onMenuShareAppMessage({
            title: '<?= $title ?>', // 分享标题
            desc: '<?= $desc ?>', // 分享描述
            link: '<?= $link ?>', // 分享链接
            imgUrl: '<?= $image ?>', // 分享图标
            type: 'link', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
                // alert(ok)
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });

    });
    <?php endif; ?>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b81df425a3bbbdb54f7033e62743687a";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>